package com.ruoyi.web.controller.comlbs;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.BigDataConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.request.realtimevideo.AlarmDataParam;
import com.ruoyi.system.domain.request.realtimevideo.GetDeviceInfoParam;
import com.ruoyi.system.domain.request.relatimepositioning.MapDeviceExportParam;
import com.ruoyi.system.domain.request.relatimepositioning.RegionDeviceParam;
import com.ruoyi.system.domain.request.reportstatistics.OriginalTrajectoryParam;
import com.ruoyi.system.domain.vo.common.ComDvoName;
import com.ruoyi.system.domain.vo.common.DeviceGps;
import com.ruoyi.system.domain.vo.realtimepositioning.MapAlarmExport;
import com.ruoyi.system.domain.vo.realtimepositioning.MapDeviceExport;
import com.ruoyi.system.domain.vo.realtimepositioning.MapTrajectoryExport;
import com.ruoyi.system.domain.vo.realtimepositioning.RegionDeviceExport;
import com.ruoyi.system.service.IEquipmentManagementService;
import com.ruoyi.system.service.IRealtimePositioningService;
import com.ruoyi.system.service.IVehicleManagementService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述 实时定位Controller
 * 作者 ZhangPing
 * 创建时间 2023/8/16 16:10
 */
@RestController
@RequestMapping("/comlbs/realtimePositioning")
public class RealtimePositioningController extends BaseController {

    @Autowired
    private IRealtimePositioningService realtimePositioningService;
    @Autowired
    private IVehicleManagementService vehicleManagementService;

    @ApiOperation("获取定位信息")
    @GetMapping("/queryLastGps")
    public AjaxResult queryLastGps(@RequestParam String deviceId) {
        return success(realtimePositioningService.queryLastGps(deviceId));
    }

    @ApiOperation("设备原始轨迹")
    @PostMapping("/trajectory/originalData")
    public AjaxResult trajectoryOriginalData(@RequestBody OriginalTrajectoryParam param) {
        return success(realtimePositioningService.originalTrajectory(param));
    }

    @ApiOperation("获取设备实时视频通道号")
    @GetMapping("/realtimeDeviceVideoList")
    public AjaxResult realtimeDeviceVideoList(@RequestParam String deviceId) {
        return success(realtimePositioningService.realtimeDeviceVideoList(deviceId));
    }

    @ApiOperation("获取地图设备列表")
    @GetMapping("/getMapDeviceList")
    public AjaxResult getMapVehicleList() {
        return success(realtimePositioningService.getMapDeviceList());
    }

    @ApiOperation("区域查车")
    @PostMapping("/regionDeviceList")
    public AjaxResult regionDeviceList(@Validated @RequestBody RegionDeviceParam param) {
        return success(realtimePositioningService.regionDeviceList(param));
    }

    @ApiOperation("区域查车导出")
    @PostMapping("/regionDeviceExport")
    public void regionDeviceExport(@RequestBody List<RegionDeviceExport> list, HttpServletResponse response) {
        new ExcelUtil<>(RegionDeviceExport.class).exportExcel(response, list, "区域查车");
    }

    @ApiOperation("地图设备信息导出")
    @Log(title = "地图设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/deviceExport")
    public void deviceExport(MapDeviceExportParam param, HttpServletResponse response) {
        List<MapDeviceExport> list = realtimePositioningService.mapDeviceExport(param);
        new ExcelUtil<>(MapDeviceExport.class).exportExcel(response, list, "设备数据");
    }

    @ApiOperation("地图设备轨迹信息导出")
    @Log(title = "地图设备轨迹信息", businessType = BusinessType.EXPORT)
    @PostMapping("/trajectoryExport")
    public void trajectoryExport(@RequestBody List<MapTrajectoryExport> list, HttpServletResponse response) {
        new ExcelUtil<>(MapTrajectoryExport.class).exportExcel(response, list, "轨迹信息");
    }

    @ApiOperation("地图报警数据导出")
    @Log(title = "地图报警数据导出", businessType = BusinessType.EXPORT)
    @PostMapping("/alarmExport")
    public void alarmExport(@RequestBody AlarmDataParam params, HttpServletResponse response) {
        List<MapAlarmExport> list = realtimePositioningService.alarmDataList(params).stream().map(d -> new MapAlarmExport()
                .setAlarmTypeName(d.getAlarmTypeName())
                .setAddress(d.getAddress())
                .setSpd(d.getSpd())
                .setStartTime(DateUtils.parseDateTime(d.getStartTime()))).collect(Collectors.toList());
        new ExcelUtil<>(MapAlarmExport.class).exportExcel(response, list, "报警数据");
    }

    @ApiOperation("设备原始轨迹导出")
    @Log(title = "设备原始轨迹导出", businessType = BusinessType.EXPORT)
    @PostMapping("/trajectory/originalDataExport")
    public void trajectoryOriginalDataExport(@RequestBody List<DeviceGps> list, HttpServletResponse response) {
        new ExcelUtil<>(DeviceGps.class).exportExcel(response, list, "轨迹数据");
    }

    @ApiOperation("获取报警数据列表")
    @PostMapping("/alarmDataList")
    public AjaxResult alarmDataList(AlarmDataParam params) {
        return success(realtimePositioningService.alarmDataList(params));
    }

    @ApiOperation("公司设备查询")
    @ApiOperationSupport(includeParameters = {"companyId","numberPlate"})
    @PostMapping("/getDeviceInfo")
    public AjaxResult getCarInfo(@Validated GetDeviceInfoParam param) {
        List<ComDvoName> resultList = vehicleManagementService.listCompanyOrVehicle(param);
        return success(resultList);
    }
}
